package com.doublefish.doublefishrobot.ui.home.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.viewpager2.widget.ViewPager2;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.doublefish.doublefishrobot.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者:dugaofeng on 2025/1/20 09:30
 */
public class Banner extends ConstraintLayout {

    Context mContext;

    private ViewPager2 viewPager;
    private BannerAdapter adapter;
    private List<HomeBannerBean> bannerBeanList = new ArrayList<>();

    public Banner(@NonNull Context context) {
        this(context, null);
    }

    public Banner(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public Banner(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initView();
        initListner();
    }

    private void initView() {
        inflate(getContext(), R.layout.view_home_banner, this);

        viewPager = findViewById(R.id.view_pager);
        adapter = new BannerAdapter(mContext, null);
        viewPager.setAdapter(adapter);
    }

    private void initListner() {
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {

                Log.i("onItemClick", "" + position);
            }
        });

        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);

                // 当滑动到最右边时，需要移动到index1的位置
                int total = bannerBeanList.size();
                if (state == ViewPager2.SCROLL_STATE_IDLE && total > 1 && viewPager.getCurrentItem() == total - 1) {
                    viewPager.setCurrentItem(1, false);
                }
                // 当滑动到最左边的时候，需要移动到index = total-2的位置
                if (state == ViewPager2.SCROLL_STATE_IDLE && total > 1 &&  viewPager.getCurrentItem() == 0) {
                    viewPager.setCurrentItem(total - 2, false);
                }
            }
        });
    }

    public void setBannerBeanList(List<HomeBannerBean> bannerBeanList) {
        if (bannerBeanList == null || bannerBeanList.size() == 0) {
            return;
        }
        this.bannerBeanList.add(bannerBeanList.get(bannerBeanList.size() - 1));
        this.bannerBeanList.addAll(bannerBeanList);
        this.bannerBeanList.add(bannerBeanList.get(0));

        adapter.setNewInstance(this.bannerBeanList);
        viewPager.setCurrentItem(1, false);
    }

    private class BannerAdapter extends BaseQuickAdapter<HomeBannerBean ,HomeBannerHolder> {
        public BannerAdapter(Context context, List<HomeBannerBean> data) {
            super(R.layout.item_home_banner, data);
            mContext = context;
        }

        @Override
        protected void convert(HomeBannerHolder helper, HomeBannerBean item) {
            helper.imageView.setImageResource(item.resId);
        }
    }



    static class HomeBannerHolder extends BaseViewHolder {
        ImageView imageView;

        public HomeBannerHolder(View view) {
            super(view);
            imageView = view.findViewById(R.id.imageview);
        }
    }

    public static class HomeBannerBean {
//        String imgPath;
//
//        public String getImgPath() {
//            return imgPath;
//        }
//
//        public void setImgPath(String imgPath) {
//            this.imgPath = imgPath;
//        }

        int resId;

        public int getResId() {
            return resId;
        }

        public void setResId(int resId) {
            this.resId = resId;
        }
    }
}
